﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEditor;


namespace KunURP.Editor
{
    public class KunEnumDrawer : KunSubDrawer
    {
        private string[] m_EnumKeys;
        private int[] m_EnumValues;
        public KunEnumDrawer(string group, string strType) : base(group)
        {
            Type enumType = null;
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in assemblies)
            {
                enumType = assembly.GetType(strType);
                if (enumType != null)
                {
                    break;
                }
            }
            if (enumType != null)
            {
                this.m_EnumKeys = Enum.GetNames(enumType);
                this.m_EnumValues = Enum.GetValues(enumType) as int[];
            }
        }
        public KunEnumDrawer(string group, string k1, float v1)
        : this(group, new string[1]{k1}, new int[1]{(int)v1}) {}
        public KunEnumDrawer(string group, string k1, float v1, string k2, float v2)
        : this(group, new string[2]{k1, k2}, new int[2]{(int)v1, (int)v2}) {}
        public KunEnumDrawer(string group, string k1, float v1, string k2, float v2, string k3, float v3)
        : this(group, new string[3]{k1, k2, k3}, new int[3]{(int)v1, (int)v2, (int)v3}) {}
        public KunEnumDrawer(string group, string k1, float v1, string k2, float v2, string k3, float v3, string k4, float v4)
        : this(group, new string[4]{k1, k2, k3, k4}, new int[4]{(int)v1, (int)v2, (int)v3, (int)v4}) {}
        public KunEnumDrawer(string group, string k1, float v1, string k2, float v2, string k3, float v3, string k4, float v4, string k5, float v5)
        : this(group, new string[5]{k1, k2, k3, k4, k5}, new int[5]{(int)v1, (int)v2, (int)v3, (int)v4, (int)v5}) {}
        public KunEnumDrawer(string group, string[] keys, int[] values) : base(group)
        {
            this.m_EnumKeys = keys;
            this.m_EnumValues = values;
        }

        public override void DrawShaderProperty(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
        {
            Rect rect = EditorGUILayout.GetControlRect();
            int index = (int)prop.floatValue;

            EditorGUI.BeginChangeCheck();
            EditorGUI.showMixedValue = prop.hasMixedValue;
            int num = EditorGUI.IntPopup(rect, label.text, index, this.m_EnumKeys, this.m_EnumValues);
            EditorGUI.showMixedValue = false;
            if (EditorGUI.EndChangeCheck())
            {
                prop.floatValue = (float)num;
            }
        }
    }
}
